//
//  AppDelegate.swift
//  PushExten
//
//  Created by zz on 20.3.24.
//

import UIKit
import AVFoundation

@main
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {

var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
//        application.registerForRemoteNotifications()
        
        UNUserNotificationCenter.current().delegate = self
        let application = UIApplication.shared
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: { _, _ in }
        )
        application.registerForRemoteNotifications()
        return true
    }
    
    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        print("token===================\(token)")
    }
    
    
        
        public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                           willPresent notification: UNNotification,
                                           withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo
            print("===========userNotificationCenter")
            speakContent("就是5")
            completionHandler(.alert)
        }
        
        
        public func userNotificationCenter(_ center: UNUserNotificationCenter,
                                           didReceive response: UNNotificationResponse,
                                           withCompletionHandler completionHandler: @escaping () -> Void) {
            //        if UIApplication.shared.applicationState == .active {
            //            return
            //        }
            let userInfo = response.notification.request.content.userInfo
            
            completionHandler()
        
    }
    
    

    func speakContent(_ content: String) {
        // 配置音频会话
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setActive(true)
            try session.setCategory(.playback)
        } catch {
            print("Failed to configure AVAudioSession: \(error)")
        }
        
        // 创建嗓音，指定语言
        guard let voice = AVSpeechSynthesisVoice(language: "zh-CN") else {
            print("Voice not available for the specified language.")
            return
        }
        
        // 创建语音合成器
        let synthesizer = AVSpeechSynthesizer()
        
        // 实例化发声的对象
        let utterance = AVSpeechUtterance(string: content)
        utterance.voice = voice
        utterance.rate = 0.5 // 设置语速
        
        // 开始朗读内容
        synthesizer.speak(utterance)
    }



}


